#!/bin/bash
#description configuration rsync server 
User="rsync_backup"
Passwd="Aa123456"
PathDir="/backup"
PasswdFile="/etc/rsyncd.passwd"
AllowIP="10.100.100.0/24"
yum -y install rsync &>/dev/null 
cat >/etc/rsyncd.conf<<EOF
#rsyncd.conf start ####
uid = rsync
gid = rsync
use chroot = no
port = 873
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
#syslog
log file = /var/log/rsyncd.log
transfer logging = no 
log format = %t %a %m %f %b
[backup]
path=$PathDir
ignore errors
read only = false
list = false
hosts allow = $AllowIP
hosts deny = 0.0.0.0/32
auth users = $User
secrets file = $PasswdFile
#rsync_config-----------------------end
EOF
if ! id rsync &>/dev/null;then
	useradd -g 873 -s /sbin/nologin rsync 
fi
[ ! -d $PathDir ] && mkdir $PathDir
[ ! -f $PasswdFile ] && touch $PasswdFile
if ! cat /etc/rsyncd.passwd |grep $User &>/dev/null;then
	echo "$User:$Passwd" > $PasswdFile
else
	echo "$User user is esxi"
fi
chown -R rsync.rsync $PathDir && chmod 600 $PasswdFile
#configure iptables allow 873 
echo "/usr/bin/rsync --daemon --config=/etc/rsyncd.conf" >>/etc/rc.local
iptables -I  INPUT 1 -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
service iptables save &>/dev/null


